package com.fr.cell.core;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.core.list.IntList;
import com.fr.cell.Grid;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.cell.clipboard.CellElementsClip;
import com.fr.cell.clipboard.ElementsTransferable;
import com.fr.cell.clipboard.FloatElementsClip;
import com.fr.report.CellElement;
import com.fr.report.FloatElement;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GridUtils
{
  public static int DRAG_NONE = 0;
  public static int DRAG_CELLSELECTION = 1;
  public static int Drag_CellSelection_BottomRight_Corner = 2;
  public static int DRAG_WIDTH = 1;
  public static int DRAG_SELECT_COLUMNS = 2;
  public static int DRAG_HEIGHT = 3;
  public static int DRAG_SELECT_ROWS = 4;

  public static Object[] getAboveFloatElementCursor(ReportPane paramReportPane, double paramDouble1, double paramDouble2)
  {
    FloatElement localFloatElement;
    Cursor localCursor;
    Object[] arrayOfObject = null;
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    Iterator localIterator = localTemplateReport.floatIterator();
    do
    {
      double d1;
      double d2;
      double d3;
      double d4;
      Rectangle2D.Double localDouble;
      label183: Rectangle2D[] arrayOfRectangle2D;
      int[] arrayOfInt;
      int i;
      while (true)
      {
        do
        {
          if (!(localIterator.hasNext()))
            break label607;
          localFloatElement = (FloatElement)localIterator.next();
          double[] arrayOfDouble = caculateFloatElementLocations(localFloatElement, ReportHelper.getColumnWidthList(localTemplateReport), ReportHelper.getRowHeightList(localTemplateReport), paramReportPane.getGrid().getVerticalValue(), paramReportPane.getGrid().getHorizontalValue());
          d1 = arrayOfDouble[0];
          d2 = arrayOfDouble[1];
          d3 = arrayOfDouble[2];
          d4 = arrayOfDouble[3];
          localDouble = new Rectangle2D.Double(arrayOfDouble[0], arrayOfDouble[1], localFloatElement.getSize().getWidth(), localFloatElement.getSize().getHeight());
          if (localGridSelection.containFloatName(localFloatElement.getName()))
            break label183;
        }
        while (!(localDouble.contains(paramDouble1, paramDouble2)));
        arrayOfObject = new Object[] { localFloatElement, new Cursor(13) };
      }
      localCursor = null;
      if (localGridSelection.getFloatNameCount() == 1)
      {
        arrayOfRectangle2D = new Rectangle2D[] { new Rectangle2D.Double(d1 - 3.0D, d2 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double((d1 + d3) / 2.0D - 3.0D, d2 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double(d3 - 3.0D, d2 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double(d3 - 3.0D, (d2 + d4) / 2.0D - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double(d3 - 3.0D, d4 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double((d1 + d3) / 2.0D - 3.0D, d4 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double(d1 - 3.0D, d4 - 3.0D, 6.0D, 6.0D), new Rectangle2D.Double(d1 - 3.0D, (d2 + d4) / 2.0D - 3.0D, 6.0D, 6.0D) };
        arrayOfInt = new int[] { 6, 8, 7, 11, 5, 9, 4, 10 };
        for (i = 0; i < arrayOfRectangle2D.length; ++i)
          if (arrayOfRectangle2D[i].contains(paramDouble1, paramDouble2))
          {
            localCursor = new Cursor(arrayOfInt[i]);
            break;
          }
      }
      if ((localDouble.contains(paramDouble1, paramDouble2)) && (localCursor == null))
        arrayOfObject = new Object[] { localFloatElement, new Cursor(13) };
    }
    while (localCursor == null);
    return new Object[] { localFloatElement, localCursor };
    label607: return arrayOfObject;
  }

  public static double[] caculateFloatElementLocations(FloatElement paramFloatElement, DynamicValueList paramDynamicValueList1, DynamicValueList paramDynamicValueList2, int paramInt1, int paramInt2)
  {
    double d1 = paramDynamicValueList1.getRangeValue(paramInt2, paramFloatElement.getColumn()) + paramFloatElement.getLeftDistance();
    double d2 = paramDynamicValueList2.getRangeValue(paramInt1, paramFloatElement.getRow()) + paramFloatElement.getTopDistance();
    double d3 = d1 + paramFloatElement.getSize().getWidth();
    double d4 = d2 + paramFloatElement.getSize().getHeight();
    return new double[] { d1, d2, d3, d4 };
  }

  public static ColumnRow getEventColumnRow(ReportPane paramReportPane, double paramDouble1, double paramDouble2)
  {
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    DynamicValueList localDynamicValueList1 = ReportHelper.getRowHeightList(localTemplateReport);
    DynamicValueList localDynamicValueList2 = ReportHelper.getColumnWidthList(localTemplateReport);
    int i = paramReportPane.getGrid().getVerticalValue();
    int j = paramReportPane.getGrid().getHorizontalValue();
    int k = paramReportPane.getGrid().getVerticalBeginValue();
    int l = paramReportPane.getGrid().getHorizontalBeginValue();
    double d1 = 0.0D;
    int i1 = 0;
    if (paramDouble1 < 0.0D)
    {
      i1 = j;
      while (true)
      {
        d1 -= localDynamicValueList1.get(i1);
        if (d1 < paramDouble1)
          break label187:
        --i1;
      }
    }
    int i2 = 0;
    for (int i3 = l; i3 < 0; ++i3)
    {
      d1 += localDynamicValueList2.get(i3);
      if (d1 > paramDouble1)
      {
        i1 = i3;
        i2 = 1;
        break;
      }
    }
    if (i2 == 0)
    {
      i1 = j;
      while (true)
      {
        d1 += localDynamicValueList2.get(i1);
        if (d1 > paramDouble1)
          break;
        ++i1;
      }
    }
    label187: double d2 = 0.0D;
    int i4 = 0;
    if (paramDouble2 < 0.0D)
    {
      i4 = i;
      while (true)
      {
        d2 -= localDynamicValueList1.get(i4);
        if (d2 < paramDouble2)
          break label318:
        --i4;
      }
    }
    int i5 = 0;
    for (int i6 = k; i6 < 0; ++i6)
    {
      d2 += localDynamicValueList1.get(i6);
      if (d2 > paramDouble2)
      {
        i4 = i6;
        i5 = 1;
        break;
      }
    }
    if (i5 == 0)
    {
      i4 = i;
      while (true)
      {
        d2 += localDynamicValueList1.get(i4);
        if (d2 > paramDouble2)
          break;
        ++i4;
      }
    }
    label318: return ColumnRow.valueOf(i1, i4);
  }

  public static ColumnRow getAdjustEventColumnRow(ReportPane paramReportPane, double paramDouble1, double paramDouble2)
  {
    ColumnRow localColumnRow = getEventColumnRow(paramReportPane, paramDouble1, paramDouble2);
    int i = Math.max(localColumnRow.getColumn(), 0);
    int j = Math.max(localColumnRow.getRow(), 0);
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    if ((!(paramReportPane.isRowEndless())) && (localColumnRow.getRow() > localTemplateReport.getRowCount() - 1))
      j = localTemplateReport.getRowCount() - 1;
    if ((!(paramReportPane.isColumnEndless())) && (localColumnRow.getColumn() > localTemplateReport.getColumnCount() - 1))
      i = localTemplateReport.getColumnCount() - 1;
    return ColumnRow.valueOf(i, j);
  }

  public static void doSelectCell(ReportPane paramReportPane, int paramInt1, int paramInt2)
  {
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    if ((!(paramReportPane.isColumnEndless())) && (paramInt1 > localTemplateReport.getColumnCount() - 1))
    {
      paramInt1 = 0;
      paramInt2 += 1;
      if ((!(paramReportPane.isRowEndless())) && (paramInt2 > localTemplateReport.getRowCount() - 1))
        paramInt2 = 0;
    }
    if ((!(paramReportPane.isColumnEndless())) && (paramInt1 < 0))
    {
      paramInt1 = localTemplateReport.getColumnCount() - 1;
      paramInt2 -= 1;
      if ((!(paramReportPane.isRowEndless())) && (paramInt2 < 0))
        paramInt2 = localTemplateReport.getRowCount() - 1;
    }
    if ((!(paramReportPane.isRowEndless())) && (paramInt2 > localTemplateReport.getRowCount() - 1))
    {
      paramInt2 = 0;
      paramInt1 += 1;
      if ((!(paramReportPane.isColumnEndless())) && (paramInt1 > localTemplateReport.getColumnCount() - 1))
        paramInt1 = 0;
    }
    if ((!(paramReportPane.isRowEndless())) && (paramInt2 < 0))
    {
      paramInt2 = localTemplateReport.getRowCount() - 1;
      paramInt1 -= 1;
      if ((!(paramReportPane.isColumnEndless())) && (paramInt1 < 0))
        paramInt1 = localTemplateReport.getColumnCount() - 1;
    }
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    localGridSelection.setType(0);
    CellElement localCellElement = localTemplateReport.getCellElement(paramInt1, paramInt2);
    if (localCellElement == null)
    {
      localGridSelection.setEditBounds(paramInt1, paramInt2, 1, 1);
      localGridSelection.setOnlyCellBounds(paramInt1, paramInt2, 1, 1);
    }
    else
    {
      localGridSelection.setEditBounds(localCellElement.getColumn(), localCellElement.getRow(), localCellElement.getColumnSpan(), localCellElement.getRowSpan());
      localGridSelection.setOnlyCellBounds(localCellElement.getColumn(), localCellElement.getRow(), localCellElement.getColumnSpan(), localCellElement.getRowSpan());
    }
    paramReportPane.fireGridSelectionChanged();
  }

  public static ElementsTransferable caculateElementsTransferable(ReportPane paramReportPane)
  {
    Object localObject1;
    Object localObject2;
    ElementsTransferable localElementsTransferable = new ElementsTransferable();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
    {
      FloatElement localFloatElement1;
      localObject1 = new FloatElementsClip();
      int j = localGridSelection.getFloatNameCount();
      int k = 2147483647;
      int l = 2147483647;
      for (int i1 = 0; i1 < j; ++i1)
      {
        localObject2 = localGridSelection.getFloatName(i1);
        localFloatElement1 = localTemplateReport.getFloatElement((String)localObject2);
        if (localFloatElement1 == null)
          break label113:
        k = Math.min(k, localFloatElement1.getColumn());
        l = Math.min(localFloatElement1.getRow(), l);
      }
      for (i1 = 0; i1 < j; ++i1)
      {
        label113: localObject2 = localGridSelection.getFloatName(i1);
        localFloatElement1 = localTemplateReport.getFloatElement((String)localObject2);
        if (localFloatElement1 == null)
          break label240:
        try
        {
          FloatElement localFloatElement2 = (FloatElement)localFloatElement1.clone();
          localFloatElement2.setColumn(localFloatElement1.getColumn() - k);
          localFloatElement2.setRow(localFloatElement1.getRow() - l);
          ((FloatElementsClip)localObject1).addFloatElement(localFloatElement2);
        }
        catch (CloneNotSupportedException localCloneNotSupportedException)
        {
          FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      }
      label470: label240: localElementsTransferable.addObject(localObject1);
    }
    else
    {
      localObject1 = localGridSelection.getFirstCellRectangle();
      CellElementsClip localCellElementsClip = new CellElementsClip(((Rectangle)localObject1).width, ((Rectangle)localObject1).height);
      Rectangle localRectangle = new Rectangle(((Rectangle)localObject1).x, ((Rectangle)localObject1).y, ((Rectangle)localObject1).width, ((Rectangle)localObject1).height);
      Iterator localIterator = localTemplateReport.intersect(((Rectangle)localObject1).x, ((Rectangle)localObject1).y, ((Rectangle)localObject1).width, ((Rectangle)localObject1).height);
      while (true)
        do
          while (true)
          {
            if (!(localIterator.hasNext()))
              break label470;
            CellElement localCellElement = (CellElement)localIterator.next();
            localObject2 = new Rectangle(localCellElement.getColumn(), localCellElement.getRow(), localCellElement.getColumnSpan(), localCellElement.getRowSpan());
            if ((!(localRectangle.equals(localObject2))) && (!(localRectangle.contains((Rectangle)localObject2))))
              break;
            localCellElementsClip.addCellElement(localCellElement.deriveCellElement(localCellElement.getColumn() - ((Rectangle)localObject1).x, localCellElement.getRow() - ((Rectangle)localObject1).y));
          }
        while (!(localRectangle.intersects((Rectangle)localObject2)));
      localElementsTransferable.addObject(localCellElementsClip);
    }
    return ((ElementsTransferable)(ElementsTransferable)localElementsTransferable);
  }

  public static ColumnRow getAdjustLastColumnRowOfReportPane(ReportPane paramReportPane)
  {
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    return ColumnRow.valueOf(Math.max(1, localTemplateReport.getColumnCount()), Math.max(1, localTemplateReport.getRowCount()));
  }

  public static IntList getGridSelectionRowIndex(GridSelection paramGridSelection)
  {
    IntList localIntList = new IntList();
    for (int i = 0; i < paramGridSelection.getCellRectangleCount(); ++i)
    {
      Rectangle localRectangle = paramGridSelection.getCellRectangle(i);
      for (int j = 0; j < localRectangle.height; ++j)
      {
        int k = localRectangle.y + j;
        if (!(localIntList.contain(k)))
          localIntList.add(k);
      }
    }
    localIntList.sort();
    return localIntList;
  }

  public static IntList getGridSelectionColumnIndex(GridSelection paramGridSelection)
  {
    IntList localIntList = new IntList();
    for (int i = 0; i < paramGridSelection.getCellRectangleCount(); ++i)
    {
      Rectangle localRectangle = paramGridSelection.getCellRectangle(i);
      for (int j = 0; j < localRectangle.width; ++j)
      {
        int k = localRectangle.x + j;
        if (!(localIntList.contain(k)))
          localIntList.add(k);
      }
    }
    localIntList.sort();
    return localIntList;
  }
}